﻿namespace Resonator.Domain.Models
{
	using System;
	using System.Collections.Generic;
	using System.Globalization;

	[Serializable]
	public class ResolvedResource
	{
		private Resource _resource;
		private bool _hasAllCulturesRequested;
		private readonly List<CultureInfo> _missingCultures = new List<CultureInfo>();

		public virtual Resource Resource
		{
			get { return _resource; }
			set { _resource = value; }
		}

		public virtual bool HasAllCulturesRequested
		{
			get { return _hasAllCulturesRequested; }
			set { _hasAllCulturesRequested = value; }
		}

		public virtual IList<CultureInfo> MissingCultures
		{
			get { return _missingCultures; }
		}

		public static ResolvedResource Create(Resource resource, IEnumerable<CultureInfo> requiredCultures)
		{
			ResolvedResource resolvedResource = new ResolvedResource();
			resolvedResource.Resource = resource;
			resolvedResource.HasAllCulturesRequested = true;

			foreach (CultureInfo requiredCulture in requiredCultures)
			{
				bool found = false;
				foreach (Translation translation in resource.Translations)
				{
					if (translation.Culture.Equals(requiredCulture))
					{
						found = true;
						break;
					}
				}
				if (!found)
				{
					resolvedResource.HasAllCulturesRequested = false;
					resolvedResource.MissingCultures.Add(requiredCulture);
				}
			}

			return resolvedResource;
		}
	}
}